/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.google.code.twiddling.core.chain;

import com.google.code.twiddling.core.command.CommandContext;

/**
 * Executor interface of Command Chain
 * 
 * @author <a href="mailto:yy.young@gmail.com">Young Yang</a>
 * @create Dec 8, 2008 4:23:06 PM
 */
public abstract class Processor {

    public Processor() {

    }

    /**
     * invoke process first, then pass commandContext to next processor
     *
     * @param commandContext
     * @throws Exception
     */
    public final void next(final CommandContext commandContext) throws Exception {
        Exception e = null;
        try {
            process(commandContext);
        }
        catch (Exception ex) {
            e = ex;
            throw e;
        }
        finally {
        	//e will be null even if ex is not, please fix it.
            if(e == null) { // no exception, next
                ChainManager.getInstance().next(commandContext);
            }
        }
    }
    
    protected abstract void process(final CommandContext commandContext) throws Exception;

}
